using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using CoreData;
using EntityData;
using Helper;
namespace Services
{

    public partial class Product_UnitChangeService
    {
       readonly IProduct_UnitChangeEntity entity;

       public Product_UnitChangeService()
       {
           this.entity = new Product_UnitChangeEntity();

       }
       #region SQLCmd
       
       #endregion
       #region Main Method

       /// <summary>
       /// Save 
       /// </summary>
       /// <param name="_model"></param>
       /// <returns></returns>
       public long Save(object _model)
       {
           try
           {
               var id = long.Parse(_model.GetType().GetProperty("ID").GetValue(_model, null).ToString());
               if (id == 0)
               {
                   return entity.Save((CoreData.Product_UnitChange)_model, Table.Product_UnitChange.ToString());
               }
               else
               {
                   return entity.Update((CoreData.Product_UnitChange)_model, Table.Product_UnitChange.ToString());
               }
           }
           catch { return -1; }
       }

       /// <summary>
       /// Get by ID
       /// </summary>
       /// <param name="_id"></param>
       /// <returns></returns>
       public CoreData.Product_UnitChange GetByID(long _id)
       {
           try
           {
               return entity.GetByCusTomSQL(String.Format(SQLCommand.GetAllProductUnitChanges,"Product_UnitChange.ID="+_id)).FirstOrDefault();
           }
           catch { return null; }
       }

       /// <summary>
       /// Get List
       /// </summary>
       /// <returns></returns>
       public IList<CoreData.Product_UnitChange> GetList()
       {
           try
           {
               return entity.GetByCusTomSQL(String.Format(SQLCommand.GetAllProductUnitChanges, "1=1")).ToList();
           }
           catch { return null; }

       }

       /// <summary>
       /// Get List with Linq
       /// </summary>
       /// <returns></returns>
       public IList<CoreData.Product_UnitChange> GetListByLINQ(Func<CoreData.Product_UnitChange, Boolean> _where)
       {
           try
           {
               return entity.GetMany(_where, Table.Product_UnitChange.ToString()).ToList();
           }
           catch { return null; }
       }
       /// <summary>
       /// Get One with Linq
       /// </summary>
       /// <returns></returns>
       public CoreData.Product_UnitChange GetOneByLINQ(Func<CoreData.Product_UnitChange, Boolean> _where)
       {
           try
           {
               return entity.Get(_where, Table.Product_UnitChange.ToString());
           }
           catch { return null; }
       }

       /// <summary>
       /// Get List with search string
       /// </summary>
       /// <returns></returns>
       public IList<CoreData.Product_UnitChange> GetList(string _searchstring)
       {
           //search theo tieu chi nao do         
           try
           {
               return entity.GetByCusTomSQL(String.Format(SQLCommand.GetAllProductUnitChanges, _searchstring)).ToList();
           }
           catch { return null; }
       }


       /// <summary>
       /// Delete
       /// </summary>
       /// <param name="_model"></param>
       /// <returns></returns>
       public bool Delete(object _model)
       {
           try
           {
               entity.Delete((CoreData.Product_UnitChange)_model, Table.Product_UnitChange.ToString());
               return true;
           }
           catch { return false; }
       }


       #endregion


       #region SQLCommand
       private string StrGetListProductID = @"SELECT PUC.UnitChangeID,PU1.Name,PUC.UnitID
                                            FROM Product_UnitChange PUC
                                            LEFT JOIN Product_Unit PU1 ON PUC.UnitChangeID=PU1.ID WHERE PUC.ProductID={0}";
       #endregion
        #region Other Method
        /// <summary>
        /// Check UnitChange is Duplicate
        /// </summary>
        /// <param name="_model">ProductID, UnitID, UnitChangeID</param>
        /// <returns>true: Is Duplicate, false: No Duplicate</returns>
       public bool CheckDuplicateUnitChange(CoreData.Product_UnitChange _model)
       { 
           try
           {
               string StrCheckDuplicateUnit = @"SELECT ID FROM Product_UnitChange";
               StrCheckDuplicateUnit += " WHERE Product_UnitChange.ProductID=" + _model.ProductID + " AND";
               StrCheckDuplicateUnit += " Product_UnitChange.UnitID=" + _model.UnitID + " AND";
               StrCheckDuplicateUnit += " Product_UnitChange.UnitChangeID=" + _model.UnitChangeID;
               _model=entity.GetByCusTomSQL(String.Format(StrCheckDuplicateUnit)).FirstOrDefault();
               if (_model==null)
                   return true;//duplicate
               else return false;//no duplicate
           }
            catch { return false; }
       }
       public IList<CoreData.Product_UnitChange> GetUnitChangeByProductID(long _productid)
       {
           try
           {
               return entity.GetByCusTomSQL(String.Format(StrGetListProductID, _productid)).ToList(); ;
           }
           catch { return null; }
       }
        #endregion 
    
    }
         
}
